/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package testing;

import clasificador.genetico.PoliticaMutacion;
import java.util.ArrayList;

/**
 *
 * @author Simply
 */
public class MutacionString implements PoliticaMutacion<ArrayList<Character>> {
    double prob;

    public MutacionString(double prob) {
        this.prob = prob;
                }

    Character randChar() {
        int k = new java.util.Random().nextInt(28);
        if( k == 26 ) return ' ';
        else return (char)(k + 'a');
    }

    @Override
    public boolean mutar(ArrayList<Character> c) {
        if( new java.util.Random().nextDouble() < this.prob) {
            int position = new java.util.Random().nextInt( c.size() * 3 + 1 );
            if( position % 3 == 0 ) {
                c.add(position/3, randChar());
            } else if (position % 3 == 1) {
                c.set(position/3, randChar());
            } else {
                c.remove(position/3);
            }
            return true;
        } else {
            return false;
        }
    }
}
